//
//  LineMarkView.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2025/2/6.
//

import SwiftUI
import Charts

struct LineMarkView: View {
    @Bindable var pathStore: PathStore
    @Binding var param: NumberParam
    @State private var lotterys: [Lottery] = []
    @State private var lotteryDates: [Date] = []
    @State private var average: Double = 0
    
    var body: some View {
        List {
            LotteryNumberLineView(lotterys: lotterys, average: average, lotteryDates: lotteryDates, lotteryNumber: .one)
                .listRowSeparator(.hidden)
            LotteryNumberLineView(lotterys: lotterys, average: average, lotteryDates: lotteryDates, lotteryNumber: .two)
                .listRowSeparator(.hidden)
            LotteryNumberLineView(lotterys: lotterys, average: average, lotteryDates: lotteryDates, lotteryNumber: .three)
                .listRowSeparator(.hidden)
            LotteryNumberLineView(lotterys: lotterys, average: average, lotteryDates: lotteryDates, lotteryNumber: .four)
                .listRowSeparator(.hidden)
            LotteryNumberLineView(lotterys: lotterys, average: average, lotteryDates: lotteryDates, lotteryNumber: .five)
                .listRowSeparator(.hidden)
        }
        .listStyle(.plain)
        .onAppear {
            lotterys = getLotterysWithCondition(startDate: param.startDate, endDate: param.endDate, front1: param.front1, front2: param.front2, front3: param.front3, front4: param.front4, front5: param.front5, hind1: param.hind1, hind2: param.hind2, ball: param.ball)
                .sorted { one, two in
                    one.yearId < two.yearId
                }
            for lottery in lotterys {
                lotteryDates.append(lottery.date)
            }
        }
    }
}

#Preview {
    LineMarkView(pathStore: .init(), param: .constant(NumberParam()))
}

class LineMarkPath: PathName { }

struct LotteryNumberLineView: View {
    let lotterys: [Lottery]
    let average: Double
    let lotteryDates: [Date]
    let lotteryNumber: LotteryNumber
    
    enum LotteryNumber: String {
        case one, two, three, four, five
    }
    
    func whichNum(lottery: Lottery) -> Int {
        switch lotteryNumber {
        case .one:
            lottery.front1
        case .two:
            lottery.front2
        case .three:
            lottery.front3
        case .four:
            lottery.front4
        case .five:
            lottery.front5
        }
    }
    
    
    var body: some View {
        VStack {
            Chart(lotterys) { lottery in
                LineMark(x: .value("时间", lottery.date, unit: .day), y: .value("1号球", whichNum(lottery: lottery)))
                    .interpolationMethod(.catmullRom)
                    .symbol(.circle)
                    .foregroundStyle(.red)
                    .offset(x: -4)
                RuleMark(y: .value("平均", average))
                    .foregroundStyle(.green)
                    //.lineStyle(StrokeStyle(lineWidth: 3))
                    .annotation(position: .top, alignment: .leading) {
                        Text("平均: \(average)")
                            .font(.body.bold())
                            .foregroundStyle(.green)
                    }
            }
            //.chartYScale(domain: [0, 35])//决定Y轴的值域
            .chartYAxis {
                //AxisMarks(values: .automatic(desiredCount: 7))//决定Y轴上有几个显眼的值
                //方法二: 直接使用值数组
                AxisMarks(preset: .aligned, position: .leading ,values: yAxisScales) {
                    AxisGridLine(stroke: StrokeStyle(lineWidth: 1))
                        .foregroundStyle(.cyan)
                    AxisValueLabel()
                        .font(.footnote)
                }
                AxisMarks(values: yAxisDomains) { //添加多少网格线
                    AxisGridLine()
                }
            }
            .chartScrollableAxes(.horizontal)
            .chartXVisibleDomain(length: 3600 * 24 * 15)//X轴每页可以看多少数据
            .chartXAxis {
                AxisMarks(preset: .aligned, position: .bottom, values: lotteryDates) {
                    AxisGridLine()
                    //AxisTick()
                    AxisValueLabel(format: .iso8601.month().day())
                        .font(.footnote)
                        
                }
            }
            .frame(height: 450)
        }
    }
}
